//
//  TotalFreeViewController.m
//  PTravel
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "TotalFreeViewController.h"

@interface TotalFreeViewController ()<UITableViewDataSource, UITableViewDelegate, FiltrateViewDelegate>
@property (retain, nonatomic)HeadSliderView *headV;
@property (retain, nonatomic)Footer *footer;
@property (assign, nonatomic)CGFloat oldOffset;
@property (retain, nonatomic)UIButton *sortButton;//排序button
@property (retain, nonatomic)UIButton *filtrateButton;//筛选button
@property (retain, nonatomic)UIView *footerView;
@property (retain, nonatomic)UIView *sortView;
@property (retain, nonatomic)UIButton *nButton;
@property (retain, nonatomic)UIButton *priceButton;
@property (retain, nonatomic)UIButton *peopleButton;
@property (retain, nonatomic)UIButton *salesButton;
@property (assign, nonatomic)NSInteger order;
@property (retain, nonatomic)filtrateView *filtrateV;
@property (retain, nonatomic)UIView *filtrateView;

@end

@implementation TotalFreeViewController

- (void)dealloc
{
    [_headV release];
    [_footer release];
    [_sortButton release];
    [_filtrateButton release];
    [_footerView release];
    [_sortView release];
    [_nButton release];
    [_priceButton release];
    [_peopleButton release];
    [_salesButton release];
    [_filtrateV release];
    [_filtrateView release];
    [_myTableView release];
    [_arr release];
    [_url release];
    [_free release];
    [_locationBUtton release];
    [_freeButton release];
    [_slider release];
    [_free release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{

    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    view.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.frame.size.height);
    //夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"night" object:nil];
}

- (void)changeColor:(NSNotificationCenter *)notification
{
    
    
    self.view.backgroundColor = CELLCOLOR;
    [self.sortButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    self.sortButton.backgroundColor = CELLCOLOR;
    [self.locationBUtton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.freeButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    self.navigationController.navigationBar.barTintColor = BARCOLOR;
    self.myTableView.backgroundColor = CELLCOLOR;
    [self.myTableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 取消渲染
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.order = 2;
    self.arr = [NSMutableArray array];
    self.navigationController.
    self.navigationController.navigationBar.translucent = YES;
    UIImage *image = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    self.extendedLayoutIncludesOpaqueBars = YES;
    [self.navigationController.navigationBar setShadowImage:image];
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.isFree = YES;
    
    //超值自由行
    self.freeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.freeButton.frame = CGRectMake(0, 64, BOUNDS.size.width/2, 30);
    [self.freeButton setTitle:@"超值自由行" forState:UIControlStateNormal];
    [self.freeButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.freeButton addTarget:self action:@selector(freeButtonActiuon) forControlEvents:UIControlEventTouchUpInside];
    self.freeButton.backgroundColor = CELLCOLOR;
    [self.view addSubview:self.freeButton];
    [_freeButton release];
    
    //当地
    self.locationBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.locationBUtton.frame = CGRectMake(BOUNDS.size.width/2, 64, BOUNDS.size.width/2, 30);
    [self.locationBUtton setTitle:@"精彩当地游" forState:UIControlStateNormal];
    [self.locationBUtton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.locationBUtton addTarget:self action:@selector(locationBUttonActiuon) forControlEvents:UIControlEventTouchUpInside];
    self.locationBUtton.backgroundColor = CELLCOLOR;
    [self.view addSubview:self.locationBUtton];
    [_locationBUtton release];
    
    //条
    self.slider = [[UIView alloc] initWithFrame:CGRectMake(BOUNDS.size.width/2-140, 64+30, 90, 2)];
    self.slider.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.slider];
    [_slider release];
    
    
    
    
    //不按系统自动适应位置
    self.automaticallyAdjustsScrollViewInsets = NO;

    
    //tableview
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 33+64, BOUNDS.size.width, BOUNDS.size.height-97) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[PriceOffTableViewCell class] forCellReuseIdentifier:@"PriceOffTableViewCell"];
    self.myTableView.backgroundColor = CELLCOLOR;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    //上拉和下拉
    [self addHeader];
    [self addFooter];

    
    

#pragma mark === footerView
    //footer
    self.footerView = [[UIView alloc] initWithFrame:CGRectMake(0, BOUNDS.size.height-49, BOUNDS.size.width, 49)];
    self.footerView.backgroundColor = CELLCOLOR;
    [self.view addSubview:self.footerView];
    [_footerView release];
    
    
    self.sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sortButton.frame = CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 49);
    [self.sortButton setTitle:@"排序" forState:UIControlStateNormal];
    [self.sortButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.sortButton addTarget:self action:@selector(sortButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.sortButton];
    [_sortButton release];
    
    
    
    UIImageView *sortImage = [[UIImageView alloc] initWithFrame:CGRectMake(50*WIDTH, 15*HEGIHT, 20*WIDTH, 20*HEGIHT)];
    sortImage.image = [UIImage imageNamed:@"sort_32.png"];
    [self.sortButton addSubview:sortImage];
    [sortImage release];
    
    
#pragma mark === filtrateView
    
    //filtrateView
    self.filtrateView = [[UIView alloc] initWithFrame:BOUNDS];
    self.filtrateView.backgroundColor = [UIColor colorWithRed:0.338 green:0.366 blue:0.404 alpha:0.300];
    self.filtrateView.hidden = YES;
    //self.filtrateView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapa = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapaAction)];
    [self.filtrateView addGestureRecognizer:tapa];
    [self.view addSubview:self.filtrateView];
    [_filtrateView release];
    
    self.filtrateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.filtrateButton.frame = CGRectMake(0, 0, BOUNDS.size.width/2, 49);
    [self.filtrateButton setTitle:@"筛选" forState:UIControlStateNormal];
    [self.filtrateButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.filtrateButton addTarget:self action:@selector(filtrateButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.footerView addSubview:self.filtrateButton];
    [_filtrateButton release];
    
    UIImageView *filtrate = [[UIImageView alloc] initWithFrame:CGRectMake(50*WIDTH, 15*HEGIHT, 20*WIDTH, 20*HEGIHT)];
    filtrate.image = [UIImage imageNamed:@"iconfont-filter.png"];
    [self.filtrateButton addSubview:filtrate];
    [filtrate release];
    
    self.filtrateV = [[filtrateView alloc] initWithFrame:CGRectMake(0, BOUNDS.size.height/2, BOUNDS.size.width, BOUNDS.size.height/2)];
    self.filtrateV.hidden = YES;
    //签代理人
    self.filtrateV.filtrateViewDelegate = self;
    [self.view addSubview:self.filtrateV];
    [_filtrateV release];
    self.filtrateV.time = @"1";
    self.filtrateV.location = @"bjtj";
    
#pragma mark === sortView
    //sortView
    self.sortView = [[UIView alloc] initWithFrame:BOUNDS];
    self.sortView.backgroundColor = [UIColor colorWithRed:0.402 green:0.436 blue:0.480 alpha:0.300];
    self.sortView.hidden = YES;
    self.sortView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.sortView addGestureRecognizer:tap];
    [self.view addSubview:self.sortView];
    [_sortView release];
    
    self.nButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nButton.frame = CGRectMake(0, BOUNDS.size.height-160, BOUNDS.size.width, 40);
    [self.nButton setTitle:@"最新上线" forState:UIControlStateNormal];
    [self.nButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.nButton setBackgroundColor:CELLCOLOR];
    [self.nButton addTarget:self action:@selector(nButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.sortView addSubview:self.nButton];
    
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 39, BOUNDS.size.width-20, 0.5)];
    aView.backgroundColor = [UIColor colorWithWhite:0.736 alpha:1.000];
    [self.nButton addSubview:aView];
    
    self.priceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.priceButton.frame = CGRectMake(0, BOUNDS.size.height-120, BOUNDS.size.width, 40);
    [self.priceButton setTitle:@"价格从低到高" forState:UIControlStateNormal];
    [self.priceButton setBackgroundColor:CELLCOLOR];
    [self.priceButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.priceButton addTarget:self action:@selector(priceButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.sortView addSubview:self.priceButton];
    
    UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(10, 39, BOUNDS.size.width-20, 0.5)];
    bView.backgroundColor = [UIColor colorWithWhite:0.736 alpha:1.000];
    [self.priceButton addSubview:bView];

    self.peopleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.peopleButton.frame = CGRectMake(0, BOUNDS.size.height-80, BOUNDS.size.width, 40);
    [self.peopleButton setTitle:@"人气" forState:UIControlStateNormal];
    [self.peopleButton setBackgroundColor:CELLCOLOR];
    [self.peopleButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.peopleButton addTarget:self action:@selector(peopleButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.sortView addSubview:self.peopleButton];
    
    UIView *cView = [[UIView alloc] initWithFrame:CGRectMake(10, 39, BOUNDS.size.width-20, 0.5)];
    cView.backgroundColor = [UIColor colorWithWhite:0.736 alpha:1.000];
    [self.peopleButton addSubview:cView];

    self.salesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.salesButton.frame = CGRectMake(0, BOUNDS.size.height-40, BOUNDS.size.width, 40);
    [self.salesButton setTitle:@"销量" forState:UIControlStateNormal];
    [self.salesButton setBackgroundColor:CELLCOLOR];
    [self.salesButton setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.salesButton addTarget:self action:@selector(salesButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.sortView addSubview:self.salesButton];
    
    
    self.view.backgroundColor = CELLCOLOR;

}

#pragma mark === 代理方法
- (void)okBtn{
    self.myTableView.scrollsToTop = YES;
    self.filtrateV.hidden = YES;
    self.filtrateView.hidden = YES;
    [self create];
}


#pragma mark === 筛选方法
- (void)filtrateButtonAction
{
    self.filtrateV.hidden = NO;
    self.filtrateView.hidden = NO;
}

- (void)tapaAction
{
    self.filtrateV.hidden = YES;
    self.filtrateView.hidden = YES;
}

#pragma mark === 排序方法
- (void)nButtonAction
{
    self.order = 2;
    self.sortView.hidden = YES;
    [self create];
    [self.nButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.priceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.peopleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.salesButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
- (void)priceButtonAction
{
    self.order = 3;
    self.sortView.hidden = YES;
    [self create];
    [self.nButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.priceButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.peopleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.salesButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
- (void)peopleButtonAction
{
    self.order = 4;
    self.sortView.hidden = YES;
    [self create];
    [self.nButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.priceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.peopleButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [self.salesButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
- (void)salesButtonAction
{
    self.order = 5;
    self.sortView.hidden = YES;
    [self create];
    [self.nButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.priceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.peopleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.salesButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}

- (void)tapAction
{
    self.sortView.hidden = YES;
}



#pragma mark === 排序
- (void)sortButtonAction
{
    self.sortView.hidden = NO;
    

}

#pragma mark === 超值自由行按钮
- (void)freeButtonActiuon
{
    self.myTableView.scrollsToTop = YES;
    [UIView animateWithDuration:0.5 animations:^{
        self.slider.frame = CGRectMake(BOUNDS.size.width/2-140, 64+30, 90, 2);
        [self.freeButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [self.locationBUtton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }];
    self.isFree = YES;
    [self create];
    
}

#pragma mark === 精彩当地游按钮
- (void)locationBUttonActiuon
{
    self.myTableView.scrollsToTop = YES;
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.locationBUtton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [self.freeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.slider.frame = CGRectMake(BOUNDS.size.width-140, 64+30, 90, 2);
    }];
    self.isFree = NO;
    [self create];
}


#pragma mark --下拉刷新
- (void)addHeader
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block TotalFreeViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getData:vc.nextPage];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    //__unsafe_unretained ViewController *vc = self;
    __unsafe_unretained TotalFreeViewController *vc = self;
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        
        // 进入刷新状态就会回调这个Block
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getData:vc.nextPage];//请求数据
        
        
    }];
}



#pragma mark === 刷新数据
- (void)create
{
    [self getData:self.nextPage];

}

#pragma mark === 请求数据
- (void)getData:(NSInteger)page
{
    
    if (self.isFree == NO) {
        self.url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?abroad=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%ld&lat=38.88252582502674&lon=121.5397000807922&order=%ld&page=%ld&product_type=2410&time=%@&track_app_channel=App%%2520Store&track_app_version=6.8.2&track_device_info=iPhone7%%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%%25209.1&type=1&v=1",self.filtrateV.location,self.countryId, self.order, page, self.filtrateV.time];
        
    }else{
        self.url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?abroad=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%ld&lat=38.88252582502674&lon=121.5397000807922&order=%ld&page=%ld&product_type=1016%%2C1018%%2C1020&time=%@&track_app_channel=App%%2520Store&track_app_version=6.8.2&track_device_info=iPhone7%%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%%25209.1&type=1&v=1", self.filtrateV.location,self.countryId, self.order, page, self.filtrateV.time];
        
    }
    [AppTools getAFN:self.url block:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        NSMutableArray *listArr = [smallDic objectForKey:@"list"];
        if (self.isUpLoading == NO) {
            [self.arr removeAllObjects];
            
        }
        for (NSMutableDictionary *dic in listArr) {
            New_discount *dis = [[New_discount alloc] init];
            [dis setValuesForKeysWithDictionary:dic];
            [self.arr addObject:dis];
        }
        
       self.navigationItem.title = [NSString stringWithFormat:@"%@特价折扣",[[smallDic objectForKey:@"area"] objectForKey:@"name"]];
        
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        [self.myTableView reloadData];
        self.oldOffset = -0.5;
    }];
    
    
}

#pragma mark === tableView方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PriceOffTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PriceOffTableViewCell" forIndexPath:indexPath];
    cell.discount = [self.arr objectAtIndex:indexPath.row];
    cell.backgroundColor = CELLCOLOR;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

#pragma mark === 向下滚动消失footer
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.y-self.oldOffset-0.5);
    if (scrollView.contentOffset.y > _oldOffset+0.5) {
        self.footerView.hidden = YES;
    }
    _oldOffset = scrollView.contentOffset.y;
}

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.footerView.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
